#include "picture.h"
#include "graphics.h"

using namespace Commands;

DiasPicture::DiasPicture()
: picture(0l)
, original_width(0)
, original_height(0)
, x_offset(0)
, y_offset(0)
, x_flip_coord(0)
, y_flip_coord(0)
{}

DiasPicture::DiasPicture(const char* path, int x, int y,  int L, int id)
: picture(0l)
, original_width(0)
, original_height(0)
, x_offset(0)
, y_offset(0)
, x_flip_coord(0)
, y_flip_coord(0)
{
    if(TPicture* pic = new TPicture(path, x, y, L, id))
    {
        picture = pic;
        original_width = pic->GetW();
        original_height = pic->GetH();
    }

    SetZOrder(L * SUBLAYERS_COUNT + id);
}

void DiasPicture::SetL(const int majorLayer, const int minorLayer)
{
    SetZOrder(majorLayer * SUBLAYERS_COUNT + minorLayer);
}

void DiasPicture::SetL(const int majorLayer)
{
    SetZOrder(majorLayer * SUBLAYERS_COUNT + (GetZOrder() % SUBLAYERS_COUNT));
}

void DiasPicture::SetId(const int minorLayer)
{
    SetZOrder(GetZOrder() / SUBLAYERS_COUNT + minorLayer);
}

void DiasPicture::SetX(const float x)
{
    AbstractPicture::SetX(x);
    picture->SetX(x + x_flip_coord);
}

void DiasPicture::SetY(const float y)
{
    AbstractPicture::SetY(y);
    picture->SetY(y + y_flip_coord);
}

void DiasPicture::SetXY(const float x, const float y)
{
    AbstractPicture::SetXY(x, y);
    picture->SetXY(x + x_flip_coord, y + y_flip_coord);
}
int DiasPicture::AccordinglyXDisplacement(void)
{
    if(flip == FLIPPED_HORIZONTAL || flip == ROTATED_180)
    {
        return original_width - 2 * x_offset - picture->GetW();
    }
    return 0;
}
int DiasPicture::AccordinglyYDisplacement(void)
{
    if(flip == FLIPPED_VERTICAL || flip == ROTATED_180)
    {
        return original_height - 2 * y_offset - picture->GetH();
    }
    return 0;
}
//
void DiasPicture::SetZOrder(const int order)
{
    AbstractPicture::SetZOrder(order);
    picture->SetL(order / SUBLAYERS_COUNT, order % SUBLAYERS_COUNT);
}

void DiasPicture::SetFlip(const IGraphicElement::Flip flip)
{
    AbstractPicture::SetFlip(flip);
    switch(flip)
    {
        case IGraphicElement::NONE:
            picture->SetOrientation(::Flip::NORMAL);
        break;
        case IGraphicElement::FLIPPED_HORIZONTAL:
            picture->SetOrientation(::Flip::FLIPPED_HORIZONTAL);
        break;
        case IGraphicElement::FLIPPED_VERTICAL:
            picture->SetOrientation(::Flip::FLIPPED_VERTICAL);
        break;
        case IGraphicElement::ROTATED_180:
            picture->SetOrientation(::Flip::ROTATED_180);
        break;
    }
    x_flip_coord = AccordinglyXDisplacement();
    y_flip_coord = AccordinglyYDisplacement();
}

void DiasPicture::SetAngle(const float angle)
{
    AbstractPicture::SetAngle(angle);
    picture->SetAngle(angle);
}

void DiasPicture::SetScale(const float scale)
{
    AbstractPicture::SetScale(scale);
    picture->SetXScale(scale);
    picture->SetYScale(scale);
}

void DiasPicture::SetXScale(const float scale)
{
    AbstractPicture::SetXScale(scale);
    picture->SetXScale(scale);
}

void DiasPicture::SetYScale(const float scale)
{
    AbstractPicture::SetYScale(scale);
    picture->SetYScale(scale);
}

void DiasPicture::SetAlpha(const float alpha)
{
    AbstractPicture::SetAlpha(alpha);
    picture->SetAlpha(AbstractPicture::alpha);
}

void DiasPicture::SetXAnchor(const float anchor)
{
    AbstractPicture::SetXAnchor(anchor);
    picture->SetXAnchor((this->original_width * anchor - x_offset) / picture->GetW());
}

void DiasPicture::SetYAnchor(const float anchor)
{
    AbstractPicture::SetYAnchor(anchor);
    picture->SetYAnchor((this->original_height * anchor - y_offset) / picture->GetH());
}

void DiasPicture::Load(const char* path)
{
    if(!picture)
    {
        picture = new TPicture();
        picture->Load(path);
        original_width = picture->GetW();
        original_height = picture->GetH();
    }
}

void DiasPicture::SetCropOffset(const int original_width, const int original_height, const int x_offset, const int y_offset)
{
    this->original_width = original_width;
    this->original_height = original_height;
    this->x_offset = x_offset;
    this->y_offset = y_offset;
}

DiasPicture::~DiasPicture()
{
    delete picture;
}

void DiasPicture::Show(void)
{
    picture->Out();
}

void DiasPicture::Hide(void)
{
    picture->Hide();
}

float DiasPicture::GetWidth(void) const
{
    return float(original_width);
}

float DiasPicture::GetHeight(void) const
{
    return float(original_height);
}
